<%= error_messages_for 'questionnaire' %>

<div id=create_questionnaire>
  <% form_for @questionnaire do |f| %>
    <p> Name: <%= f.text_field :name %>               </p>
    <p> Maximum time: 
    <span id=timed_questionnaire_checkbox>
      Timed? <input id="timed_questionnaire" name="timed_questionnaire" type="checkbox" 
               value="1" onclick="toggle_and_reset('questionnaire_hours'); toggle_and_reset('questionnaire_minutes');" />
    </span>
    <%= text_field_tag 'questionnaire[hours]', '', :disabled => true %> hours <%= text_field_tag 'questionnaire[minutes]', '', :disabled => true %> minutes
    </p>
    <p>
      Wrong answer penalty: <%= text_field_tag 'questionnaire[wrong_answer_penalty]', '' %> 
      <span id=statistical_penalty_checkbox>
        Statistical penalty <input id="statistical_penalty" name="statistical_penalty" type="checkbox" checked=1
        value="1" onclick="toggle_and_set_wrong_penalty('questionnaire_wrong_answer_penalty');" />
      </span>
    </p>
    <p> No answer penalty: <%= f.text_field :no_answer_penalty %> </p>
    <label for="select_questions">Questions:</label>
    <%= collection_multiple_select :questionnaire, :question_ids, @questions, :id, :wording, {:outer_class => 'multiple_select', :inner_class => 'multiple_select_checkbox'} %>
    <%= tree_multiple_select(:questionnaire, :category_ids, @root_categories, :id, :name)%>
    <p> <%= submit_tag "Create questionnaire" %>      </p>
  <% end %>
</div>


<div id=links_section>
  <%= link_to 'Back to \'List of Questionnaires\'', questionnaires_path %>
</div>

<script type="text/javascript">
  
function toggle_and_reset(id) {
  if (document.getElementById(id).disabled) {       // toggle
    document.getElementById(id).disabled = false;
  } else {
    document.getElementById(id).disabled = true;
  }
  document.getElementById(id).value = ""            // reset
}

function set_wrong_penalty_checkbox_disabled_properties(id) {
  backColor = document.getElementById(id).style.backgroundColor;
  if (backColor == "") { backColor = 'white'; }
  document.getElementById(id).style.color = backColor;
  document.getElementById(id).value = -1;
}

function toggle_and_set_wrong_penalty(id) {
  if (document.getElementById(id).disabled) {
    document.getElementById(id).disabled = false;
    document.getElementById(id).style.color = 'black';
    document.getElementById(id).value = 0
  } else {
    document.getElementById(id).disabled = true;
    set_wrong_penalty_checkbox_disabled_properties('questionnaire_wrong_answer_penalty');
  }
}
if (document.getElementById('questionnaire_wrong_answer_penalty').disabled) {
  set_wrong_penalty_checkbox_disabled_properties('questionnaire_wrong_answer_penalty');
} else {
  if (document.getElementById('statistical_penalty').checked) {
    toggle_and_set_wrong_penalty('questionnaire_wrong_answer_penalty');    
  }
}

</script>
